package com.example.chapter01;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn1 = findViewById(R.id.btn_finish);
        btn1.setOnClickListener(this);
        Button btn2 = findViewById(R.id.btn_jump2second); // jump to SecondActivity by class
        btn2.setOnClickListener(this);
        Button btn3 = findViewById(R.id.btn_jump2action); // jump to SecondActivity by ACTION_START
        btn3.setOnClickListener(this);
        Button btn4 = findViewById(R.id.btn_jump2web);  // jump to web page by ACTION_VIEW
        btn4.setOnClickListener(this);
        Button btn5 = findViewById(R.id.btn_jump2third); // jump to ThirdActivity
        btn5.setOnClickListener(this);

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.add_item) {
            Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
        } else if (item.getItemId() == R.id.remove_item) {
            Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_finish) {
            finish();
        } else if (v.getId() == R.id.btn_jump2second) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_jump2action) {
            Intent intent = new Intent("android.intent.action.ACTION_START");
            intent.addCategory("android.intent.category.MY_CATEGORY");
            startActivity(intent);
        } else if (v.getId() == R.id.btn_jump2web) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://www.baidu.com"));
            startActivity(intent);
        } else if (v.getId() == R.id.btn_jump2third) {
            Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
            startActivity(intent);
        }
    }
}